import type { PageResult, PageParams, BaseResponse } from '@/types/api.d'
import request from '@/utils/request'
import { ElMessage } from 'element-plus'

/**
 * SSH配置类型定义
 */
export interface SshConfig {
  id: number
  name: string
  host: string
  port: number
  username: string
  password?: string
  privateKey?: string
  hospitalId: number
  description?: string
  createTime: string
  updateTime: string
}

/**
 * SSH配置查询参数
 */
export interface SshConfigSearchParams extends PageParams {
  name?: string
  host?: string
  hospitalId?: number
}

/**
 * SSH连接测试参数
 */
export interface SshTestParams {
  host: string
  port: number
  username: string
  password?: string
  privateKey?: string
}

/**
 * 获取SSH配置列表
 * @param params 查询参数
 * @returns 分页结果
 */
export const fetchSshConfigs = async (params: SshConfigSearchParams): Promise<PageResult<SshConfig>> => {
  try {
    const { data } = await request.get<BaseResponse<PageResult<SshConfig>>>('/api/ssh-configs', { params })
    return data.data
  } catch (error) {
    ElMessage.error('获取SSH配置列表失败')
    throw error
  }
}

/**
 * 获取SSH配置详情
 * @param id 配置ID
 * @returns 配置详情
 */
export const getSshConfig = async (id: number): Promise<SshConfig> => {
  try {
    const { data } = await request.get<BaseResponse<SshConfig>>(`/api/ssh-configs/${id}`)
    return data.data
  } catch (error) {
    ElMessage.error('获取SSH配置详情失败')
    throw error
  }
}

/**
 * 创建SSH配置
 * @param data 配置数据
 * @returns 创建的配置信息
 */
export const createSshConfig = async (data: Omit<SshConfig, 'id'>): Promise<SshConfig> => {
  try {
    const { data: response } = await request.post<BaseResponse<SshConfig>>('/api/ssh-configs', data)
    ElMessage.success('创建SSH配置成功')
    return response.data
  } catch (error) {
    ElMessage.error('创建SSH配置失败')
    throw error
  }
}

/**
 * 更新SSH配置
 * @param data 配置数据
 * @returns 更新后的配置信息
 */
export const updateSshConfig = async (data: SshConfig): Promise<SshConfig> => {
  try {
    const { data: response } = await request.put<BaseResponse<SshConfig>>(`/api/ssh-configs/${data.id}`, data)
    ElMessage.success('更新SSH配置成功')
    return response.data
  } catch (error) {
    ElMessage.error('更新SSH配置失败')
    throw error
  }
}

/**
 * 删除SSH配置
 * @param id 配置ID
 */
export const deleteSshConfig = async (id: number): Promise<void> => {
  try {
    await request.delete(`/api/ssh-configs/${id}`)
    ElMessage.success('删除SSH配置成功')
  } catch (error) {
    ElMessage.error('删除SSH配置失败')
    throw error
  }
}

/**
 * 测试SSH连接
 * @param params 连接参数
 */
export const testSshConnection = async (params: SshTestParams): Promise<void> => {
  try {
    await request.post('/api/ssh-configs/test-connection', params)
    ElMessage.success('SSH连接测试成功')
  } catch (error) {
    ElMessage.error('SSH连接测试失败')
    throw error
  }
}

// 缓存最近请求的配置数据
const sshConfigCache = new Map<number, SshConfig>()

/**
 * 获取SSH配置详情（带缓存）
 * @param id 配置ID
 * @param forceRefresh 是否强制刷新缓存
 * @returns 配置详情
 */
export const getSshConfigWithCache = async (id: number, forceRefresh = false): Promise<SshConfig> => {
  if (!forceRefresh && sshConfigCache.has(id)) {
    return sshConfigCache.get(id)!
  }

  const config = await getSshConfig(id)
  sshConfigCache.set(id, config)
  return config
}
